<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
</body>

</html>
<script>
    var arr = [{
        name: '李白',
        age: 18,
        gender: '男'
    }, {
        name: '公孙离',
        age: 20,
        gender: '女'
    }, {
        name: '元歌',
        age: 22,
        gender: '男'
    }]

    function fnArr(arr) {
        var body = document.querySelector('body')
        var table = document.createElement('table')
        body.appendChild(table)
        for (var i = 0; i < arr.length; i++) {
            var tr = document.createElement('tr')
            for (var key in arr[i]) {
                var td = document.createElement('td')
                tr.appendChild(td)
                td.innerText = arr[i][key]
                td.style.borderWidth = '1px'
                td.style.borderStyle = 'solid'
            }
            var btn = document.createElement('button')
            var lastTd = document.createElement('td')
            btn.innerText = '删除'
                // btn.index = i
            table.onclick = function(e) {
                if (e.target.localName === "button") {
                    //说明点的是删除按钮
                    //通过button上面绑定的index属性 从数组中删除该项
                    arr.splice(e.target.index, 1);
                    body.innerHTML = ''
                    fnArr(arr)
                }
            }

            tr.appendChild(lastTd)
            lastTd.appendChild(btn)
            table.appendChild(tr)
            lastTd.style.borderWidth = '1px'
            lastTd.style.borderStyle = 'solid'
        }
        table.style.borderWidth = '1px'
        table.style.borderStyle = 'solid'
        table.style.borderSpacing = '0'
        table.style.width = '400px'
        table.style.height = '200px'
    }
    fnArr(arr);
</script>